package com.test.schoolhelper.ui.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;

import com.test.schoolhelper.R;
import com.test.schoolhelper.base.BaseActivity;
import com.test.schoolhelper.databinding.ActivityUserDetailsBinding;
import com.test.schoolhelper.db.DbUserController;
import com.test.schoolhelper.entity.User;
import com.test.schoolhelper.event.UserEvent;
import com.test.schoolhelper.utils.ToastUtils;

import org.greenrobot.eventbus.EventBus;

// TODO: 2024/6/1 联系人详情
public class UserDetailsActivity extends BaseActivity<ActivityUserDetailsBinding> implements View.OnClickListener {


    private User user;
    private DbUserController dbUserController;

    @Override
    protected void initData() {
        super.initData();
        dbUserController = DbUserController.getInstance(this);
        user = (User) getIntent().getSerializableExtra("data");
        getBinding().etName.setText(user.getName());
        getBinding().etPhone.setText(user.getPhone());
        getBinding().etCompany.setText(user.getCompany());
        getBinding().etAddress.setText(user.getAddress());
    }

    @Override
    protected void initClick() {
        super.initClick();
        getBinding().tvSave.setOnClickListener(this);
        getBinding().tvDelete.setOnClickListener(this);
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_user_details;
    }

    @Override
    protected ActivityUserDetailsBinding onCreateViewBinding(@NonNull LayoutInflater layoutInflater) {
        return ActivityUserDetailsBinding.inflate(layoutInflater);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_delete:
                delete();
                break;
            case R.id.tv_save:
                save();
                break;
            case R.id.iv_back:
                finish();
                break;
        }
    }

    private void delete() {
        dbUserController.delete(user.getName());
        EventBus.getDefault().post(new UserEvent());
        new Handler().postDelayed(() -> {
            ToastUtils.getInstance().show(this, "删除成功！");
            finish();
        }, 500);
    }

    private void save() {
        String name = getBinding().etName.getText().toString();
        String phone = getBinding().etPhone.getText().toString();
        if (name.isEmpty() || phone.isEmpty()) {
            ToastUtils.getInstance().show(this, "姓名和电话不能为空！");
            return;
        }
        String company = getBinding().etCompany.getText().toString();
        String address = getBinding().etAddress.getText().toString();
        user.setName(name);
        user.setPhone(phone);
        user.setAddress(address);
        user.setCompany(company);
        dbUserController.update(user);
        EventBus.getDefault().post(new UserEvent());
        new Handler().postDelayed(() -> {
            ToastUtils.getInstance().show(this, "保存成功！");
            finish();
        }, 500);

    }
}